<?php
/**
 * 注意：本内容仅限于博也公司内部传阅,禁止外泄以及用于其他的商业目的
 * @author    hebidu<346551990@qq.com>
 * @copyright 2018 www.itboye.com Boye Inc. All rights reserved.
 * @link      http://www.itboye.com/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 * Revision History Version
 ********1.0.0********************
 * file created @ 2018-01-09 15:55
 *********************************
 ********1.0.1********************
 *
 *********************************
 */

namespace app\admin\controller;


use app\src\sunsun\common\logic\AllDeviceLocLogic;

class DeviceMap extends Admin
{
    public function index()
    {
        $dType = $this->_param('d_type', '');
        $day = $this->_param('day', 1);
        $did = $this->_param('did', '');
        $delay = $this->_param('delay', 0);
        $ip = $this->_param('ip', '');

        $day = intval($day);
        if ($day != 1 && $day != 30 && $day != 180) {
            $day = 1;
        }
        $map = [
            'geohash' => ['neq', ''],
            'last_login_time' => ['gt', time() - $day * 24 * 3600],
        ];
        if (!empty($dType)) {
            $map['d_type'] = $dType;
        }
        if (!empty($did)) {
            $map['did'] = ['like', '%'.$did.'%'];
        }
        if (!empty($delay)) {
            $map['delay'] = ['gt', $delay];
        }
        if (!empty($ip)) {
            $map['ip'] = $ip;
        }
        $this->assign('ip', $ip);
        $this->assign('delay', $delay);
        $this->assign('did', $did);
        $this->assign('d_type', $dType);
        $this->assign('day', $day);
        $result = (new AllDeviceLocLogic())->queryNoPaging($map, 'last_login_time asc');
        if ($result['status']) {
            $data = $result['info'];
            $list = [];
            $todayList = [];
            $maxDelay = 1;
            $ipCache = [];
            $locCache = [];
            $delta = 0.0001;

            foreach ($data as $vo) {
                if ($vo['delay'] > $maxDelay) $maxDelay = $vo['delay'];
                $key = $vo['ip'];
                $lng = floatval($vo['lng']);
                $lat = floatval($vo['lat']);
                $locKey = $lng.'_'.$lat;

                // 处理存在同ip的情况
                // 进行经纬度 以delta的增量进行递增
                // 进行位置模拟
                if (array_key_exists($key, $ipCache)) {
                    $deltaLoc = $ipCache[$key];
                    if ($deltaLoc['lng'] > $deltaLoc['o_lng'] - 8*$delta) {
                        $lng = floatval($deltaLoc['lng']) - $delta;
                        $ipCache[$key]['lng'] = $lng;
                    }elseif ($deltaLoc['lat'] > $deltaLoc['o_lat'] - 8*$delta) {
                        $lat = floatval($deltaLoc['lat']) - $delta;
                        $ipCache[$key]['lat'] = $lat;
                    } else {
                        $lat = floatval($deltaLoc['lat']) - ($delta / 3);
                        $lng = floatval($deltaLoc['lng']) - ($delta / 3);
                        $ipCache[$key]['lat'] = $lat;
                        $ipCache[$key]['lng'] = $lng;
                    }
                } else {
                    $ipCache[$key] = [
                        'o_lng' => $lng,
                        'o_lat' => $lat,
                        'lng' => $lng,
                        'lat' => $lat
                    ];

                    // 处理 不同ip但同位置的情况
                    // 进行经纬度 以delta的增量进行递减
                    if (array_key_exists($locKey, $locCache)) {
                        $deltaLoc = $locCache[$locKey];
                        if ($deltaLoc['lng'] < $deltaLoc['o_lng'] + 8 * $delta) {
                            $lng = floatval($deltaLoc['lng']) + $delta;
                            $locCache[$locKey]['lng'] = $lng;
                        }elseif ($deltaLoc['lat'] < $deltaLoc['o_lat'] + 8 * $delta) {
                            $lat = floatval($deltaLoc['lat']) + $delta;
                            $locCache[$locKey]['lat'] = $lat;
                        } else {
                            $lat = floatval($deltaLoc['lat']) + ($delta / 3);
                            $lng = floatval($deltaLoc['lng']) + ($delta / 3);
                            $ipCache[$locKey]['lat'] = $lat;
                            $ipCache[$locKey]['lng'] = $lng;
                        }
                    } else {
                        $locCache[$locKey] = [
                            'o_lng' => $lng,
                            'o_lat' => $lat,
                            'lng' => $lng,
                            'lat' => $lat
                        ];

                    }
                }
                $tmp = [
                    'name' => '[' . $vo['d_type'] . ']',
                    'value' => [
                        $lng,
                        $lat,
                        number_format($vo['delay'], 2, ".", ""),
                        $vo['did'],
                        $vo['city'],
                        $vo['ip'],
                        toDatetime($vo['last_login_time'])
                    ]
                ];


                array_push($list, $tmp);

            }


            $this->assign('total', count($list) + count($todayList));
            $this->assign('max_delay', $maxDelay);
            $this->assign('today_data', json_encode($todayList));
            $this->assign('data', json_encode($list));
        } else {
            $this->error($result['info']);
        }
        return $this->boye_display();
    }

    protected function _initialize()
    {

    }
}